import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../features/home/presentation/pages/home_page.dart';
import '../features/profile/presentation/pages/profile_page.dart';
import '../features/settings/presentation/pages/settings_page.dart';
import '../features/list/presentation/pages/example_list_page.dart';
import '../features/list/presentation/pages/simple_list_page.dart';
import '../features/tab/presentation/pages/tab_page.dart';
import '../shared/widgets/splash_screen.dart';
import '../core/constants/app_constants.dart';

class AppRoutes {
  static const String splash = '/splash';
  static const String home = AppConstants.routeHome;
  static const String login = AppConstants.routeLogin;
  static const String profile = AppConstants.routeProfile;
  static const String settings = AppConstants.routeSettings;
  static const String about = AppConstants.routeAbout;
  static const String list = '/list';
  static const String mainTab = '/main-tab';
  
  // 子路由
  static const String profileEdit = '/profile/edit';
  static const String settingsTheme = '/settings/theme';
  static const String settingsLanguage = '/settings/language';
  static const String settingsNotification = '/settings/notification';
  static const String settingsAbout = '/settings/about';
  
  // 错误页面
  static const String notFound = '/not-found';
  static const String error = '/error';
  
  static List<GetPage> get pages => [
    // 启动页
    GetPage(
      name: splash,
      page: () => const SplashScreen(),
      transition: Transition.fade,
      transitionDuration: const Duration(milliseconds: 300),
    ),
    
    // 主页
    GetPage(
      name: home,
      page: () => const HomePage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 个人资料页
    GetPage(
      name: profile,
      page: () => const ProfilePage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 设置页
    GetPage(
      name: settings,
      page: () => const SettingsPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 列表页
    GetPage(
      name: list,
      page: () => const SimpleListPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
    ),
    
    // 主Tab页
    GetPage(
      name: mainTab,
      page: () => const TabPage(),
      transition: Transition.fade,
      transitionDuration: AppConstants.animationDuration,
    ),
    
    // 个人资料编辑页
    GetPage(
      name: profileEdit,
      page: () => const ProfileEditPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 主题设置页
    GetPage(
      name: settingsTheme,
      page: () => const ThemeSettingsPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 语言设置页
    GetPage(
      name: settingsLanguage,
      page: () => const LanguageSettingsPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 通知设置页
    GetPage(
      name: settingsNotification,
      page: () => const NotificationSettingsPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
      // middlewares: [AuthMiddleware()],
    ),
    
    // 关于页面
    GetPage(
      name: settingsAbout,
      page: () => const AboutPage(),
      transition: Transition.rightToLeft,
      transitionDuration: AppConstants.animationDuration,
    ),
    
    // 错误页面
    GetPage(
      name: notFound,
      page: () => const NotFoundPage(),
      transition: Transition.fade,
    ),
    
    GetPage(
      name: error,
      page: () => const ErrorPage(),
      transition: Transition.fade,
    ),
  ];
  
  // 获取初始路由
  static String getInitialRoute() {
    // 这里可以根据用户登录状态决定初始路由
    return home;
  }
  
  // 未知路由处理
  static GetPage get unknownRoute => GetPage(
    name: notFound,
    page: () => const NotFoundPage(),
    transition: Transition.fade,
  );
}

// 路由管理器
class RouteManager {
  static void toNamed(String routeName, {dynamic arguments}) {
    Get.toNamed(routeName, arguments: arguments);
  }
  
  static void offNamed(String routeName, {dynamic arguments}) {
    Get.offNamed(routeName, arguments: arguments);
  }
  
  static void offAllNamed(String routeName, {dynamic arguments}) {
    Get.offAllNamed(routeName, arguments: arguments);
  }
  
  static void back([dynamic result]) {
    Get.back(result: result);
  }
  
  static void offAll(String routeName, {dynamic arguments}) {
    Get.offAllNamed(routeName, arguments: arguments);
  }
  
  static void until(String routeName) {
    Get.until((route) => route.settings.name == routeName);
  }
  
  static String? get currentRoute => Get.currentRoute;
  
  static String? get previousRoute => Get.previousRoute;
  
  static bool get canBack => Get.key.currentState?.canPop() ?? false;
  
  static dynamic get arguments => Get.arguments;
  
  static Map<String, String?> get parameters => Get.parameters;
  
  static void clearRouteTree() {
    Get.reset();
  }
}

// 路由观察者
class RouteObserver extends GetObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);
    print('Pushed route: ${route.settings.name}');
  }
  
  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    print('Popped route: ${route.settings.name}');
  }
  
  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didRemove(route, previousRoute);
    print('Removed route: ${route.settings.name}');
  }
  
  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    print('Replaced route: ${oldRoute?.settings.name} -> ${newRoute?.settings.name}');
  }
}

// 占位页面组件
class ProfileEditPage extends StatelessWidget {
  const ProfileEditPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('编辑个人资料')),
      body: const Center(child: Text('个人资料编辑页面')),
    );
  }
}

class ThemeSettingsPage extends StatelessWidget {
  const ThemeSettingsPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('主题设置')),
      body: const Center(child: Text('主题设置页面')),
    );
  }
}

class LanguageSettingsPage extends StatelessWidget {
  const LanguageSettingsPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('语言设置')),
      body: const Center(child: Text('语言设置页面')),
    );
  }
}

class NotificationSettingsPage extends StatelessWidget {
  const NotificationSettingsPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('通知设置')),
      body: const Center(child: Text('通知设置页面')),
    );
  }
}

class AboutPage extends StatelessWidget {
  const AboutPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('关于')),
      body: const Center(child: Text('关于页面')),
    );
  }
}

class NotFoundPage extends StatelessWidget {
  const NotFoundPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('页面未找到')),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.error_outline, size: 64, color: Colors.grey),
            SizedBox(height: 16),
            Text('页面未找到', style: TextStyle(fontSize: 18)),
            SizedBox(height: 8),
            Text('您访问的页面不存在', style: TextStyle(color: Colors.grey)),
          ],
        ),
      ),
    );
  }
}

class ErrorPage extends StatelessWidget {
  const ErrorPage({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('发生错误')),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.error, size: 64, color: Colors.red),
            SizedBox(height: 16),
            Text('发生错误', style: TextStyle(fontSize: 18)),
            SizedBox(height: 8),
            Text('请稍后重试', style: TextStyle(color: Colors.grey)),
          ],
        ),
      ),
    );
  }
}